﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject.Web.Mvc;
using Ninject;
using System.Reflection;
using log4net;

namespace Subtle
{
	// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
	// visit http://go.microsoft.com/?LinkId=9394801

	public class MvcApplication : NinjectHttpApplication
	{
		private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

		/// <summary>
		/// Initializes a new MVC Application
		/// </summary>
		public MvcApplication()
		{
			this.EndRequest += new EventHandler(OnEndRequest);
		}

		/// <summary>
		/// On End Request
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void OnEndRequest(object sender, EventArgs e)
		{
		}

		/// <summary>
		/// Register Global Filters
		/// </summary>
		/// <param name="filters"></param>
		public static void RegisterGlobalFilters(GlobalFilterCollection filters)
		{
			filters.Add(new HandleErrorAttribute());
		}

		/// <summary>
		/// Register Routes
		/// </summary>
		/// <param name="routes"></param>
		public static void RegisterRoutes(RouteCollection routes)
		{
			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			routes.MapRoute(
				"PostArchives", // Route name
				"Post/{action}/{year}/{month}", // URL with parameters
				new { controller = "Post", action = "Posts", year = UrlParameter.Optional, month = UrlParameter.Optional },
				new { year = @"\d+", month = @"\d+" },
				new string[] { "Subtle.Controllers" }
			);

			routes.MapRoute(
				"MediaSpecific", // Route name
				"Media/{id}", // URL with parameters
				new { controller = "Media", action = "Show", id = UrlParameter.Optional },
				new string[] { "Subtle.Controllers" }
			);

			routes.MapRoute(
				"PostSpecific", // Route name
				"Post/{id}", // URL with parameters
				new { controller = "Post", action = "Index"},
				new string[] { "Subtle.Controllers" }
			);

			routes.MapRoute(
				"Default", // Route name
				"{controller}/{action}/{id}", // URL with parameters
				new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
			);

		}

		/// <summary>
		/// Create Kernel
		/// </summary>
		/// <returns></returns>
		protected override IKernel CreateKernel()
		{
			var kernel = new StandardKernel();
			kernel.Load(Assembly.GetExecutingAssembly());
			return kernel;
		}

		/// <summary>
		/// On Application Started
		/// </summary>
		protected override void OnApplicationStarted()
		{
			base.OnApplicationStarted();

			AreaRegistration.RegisterAllAreas();
			RegisterGlobalFilters(GlobalFilters.Filters);
			RegisterRoutes(RouteTable.Routes);

			log4net.Config.XmlConfigurator.Configure();
			logger.Debug("Logging Initialized");
			//RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
		}
	}
}